﻿using Swimj.DomainLib.Entities;

namespace Production.DomainLib.AggregatesModel.TechnologyAggregate
{
    public class Technology : AuditableAggregateRoot<int>
    {
        /// <summary>
        /// 工艺路线编码
        /// </summary>
        /// <value></value>
        public string? Code { get; set; }

        /// <summary>
        /// 工艺路线名称
        /// </summary>
        /// <value></value>
        public string? Name { get; set; }

        /// <summary>
        /// 流程类型
        /// </summary>
        /// <value></value>
        public int ProcessType { get; set; }

        /// <summary>
        /// 生产类型
        /// </summary>
        /// <value></value>
        public int ProductionType { get; set; }

        /// <summary>
        /// 生产产量
        /// </summary>
        public double Quality { get; set; }

        public int StandardBomId { get; set; }

        public string? StandardBomCode { get; set; }

        public string? StandardBomName { get; set; }

        /// <summary>
        /// 作业序号
        /// </summary>
        /// <value></value>
        public int Sort { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        /// <value></value>
        public string? Description { get; set; }

        public readonly List<TechnologyDetail> _technologyDetails;

        public IReadOnlyCollection<TechnologyDetail> TechnologyDetails => _technologyDetails;

        public Technology()
        {
            _technologyDetails = new List<TechnologyDetail>();
        }

        public Technology(string? code, string? name, double quality,
            int standardBomId, string? standardBomCode, string? standardBomName,
            string? description) : this()
        {
            Code = code;
            Name = name;
            Quality = quality;
            StandardBomId = standardBomId;
            StandardBomCode = standardBomCode;
            StandardBomName = standardBomName;
            Description = description;
        }

        public void Update(string? code, string? name, double quality,
            int standardBomId, string? standardBomCode, string? standardBomName,
            string? description)
        {
            Code = code;
            Name = name;
            Quality = quality;
            StandardBomId = standardBomId;
            StandardBomCode = standardBomCode;
            StandardBomName = standardBomName;
            Description = description;
        }

        public void AddItem(int processId, string? processCode,
            string? processName, int sort, double takeTime)
        {
            var technologyDetail = new TechnologyDetail(processId, processCode, processName, sort, takeTime);
            _technologyDetails.Add(technologyDetail);
        }

        public void UpdateItem(int id, int processId, string? processCode,
            string? processName, int sort, double takeTime)
        {
            var technologyDetail = _technologyDetails.FirstOrDefault(i => i.Id == id);
            technologyDetail?.Update(processId, processCode, processName, sort, takeTime);
        }
    }
}